package vn.co.thn.android.videohot.fragment;

import java.util.ArrayList;

import com.android.volley.VolleyError;

import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.adapter.VHVideoListAdapter;
import vn.co.thn.android.videohot.common.VHCommonFragment;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import vn.co.thn.android.videohot.json.VHJsonVideoList;
import vn.co.thn.android.videohot.view.PullAndLoadListView;
import vn.co.thn.android.videohot.view.PullAndLoadListView.OnLoadMoreListener;
import vn.co.thn.android.videohot.view.PullToRefreshListView.OnRefreshListener;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class VHVideoListFragment extends VHCommonFragment implements OnItemClickListener, OnRefreshListener, OnLoadMoreListener {
	private PullAndLoadListView list_video;
	private VHVideoListAdapter adapter;
	private ArrayList<VHVideoItemData> mListData = new ArrayList<VHVideoItemData>();
	private int mPage = 0;
	private String Url_api;

	public static VHVideoListFragment newInstance(String url) {
		VHVideoListFragment fragment = new VHVideoListFragment();
		Bundle data = new Bundle();
		data.putString(VHConstants.EXTRA_URL_API, url);
		fragment.setArguments(data);
		return fragment;
	}

	public static VHVideoListFragment newInstance(String url, String category_key) {
		VHVideoListFragment fragment = new VHVideoListFragment();
		Bundle data = new Bundle();
		data.putString(VHConstants.EXTRA_URL_API, url);
		data.putString(VHConstants.EXTRA_CATEGORY_ID, category_key);
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Url_api = getArguments().getString(VHConstants.EXTRA_URL_API);
		adapter = new VHVideoListAdapter(getActivity(), mListData);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.vh_video_new_fragment, container, false);
		list_video = (PullAndLoadListView) view.findViewById(R.id.lst);
		list_video.setOnItemClickListener(this);
		list_video.setOnLoadMoreListener(this);
		list_video.setOnRefreshListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		list_video.setAdapter(adapter);
		LoadTimeLine();
	}

	private void LoadTimeLine() {
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		this.mRequestService.setUrl(Url_api);
		this.mRequestService.setParams(params);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (adapter == null) {
					mListData.addAll(data.getVideo_list());
					adapter = new VHVideoListAdapter(getActivity(), mListData);
					list_video.setAdapter(adapter);
				} else {
					mListData.clear();
					mListData.addAll(data.getVideo_list());
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.Post();
	}

	@Override
	public void onDataRefresh() {
		// TODO Auto-generated method stub
		onLoadMore();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		mPage = mPage + 1;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		this.mRequestService.setUrl(Url_api);
		this.mRequestService.setParams(params);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (data.getVideo_list().size() == 0) {
					mPage = mPage - 1;
				} else {
					mListData.addAll(data.getVideo_list());
					adapter.notifyDataSetChanged();
				}
				list_video.onLoadMoreComplete();
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.Post();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		this.mRequestService.setUrl(Url_api);
		this.mRequestService.setParams(params);
		this.mRequestService.setRequestCallBack(new RequestCallBack<VHJsonVideoList>() {

			@Override
			public void onResult(VHJsonVideoList data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if (adapter == null) {
					mListData.addAll(data.getVideo_list());
					adapter = new VHVideoListAdapter(getActivity(), mListData);
					list_video.setAdapter(adapter);
				} else {
					mListData.clear();
					mListData.addAll(data.getVideo_list());
					adapter.notifyDataSetChanged();
				}
				list_video.onRefreshComplete();
			}

			@Override
			public void onResult(ArrayList<VHJsonVideoList> data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

		});
		this.mRequestService.Post();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onError(VolleyError error) {
		// TODO Auto-generated method stub
		super.onError(error);
		list_video.onLoadMoreComplete();
		list_video.onRefreshComplete();
	}

	@Override
	public void onException(Exception e) {
		// TODO Auto-generated method stub
		super.onException(e);
		list_video.onLoadMoreComplete();
		list_video.onRefreshComplete();
	}
}
